#include "BreadthFirstSearch.h"
#include <iostream>
#include <time.h>

using namespace std;

const int X_COUNT = 20, Y_COUNT = 10;

void showMapData1(const int (&data)[Y_COUNT][X_COUNT])
{
    for (int i = 0; i < X_COUNT; ++i)
    {
        cout << "▁"; // ─
    }
    cout << endl;

    for (int y = 0; y < Y_COUNT; ++y)
    {
        for (int x = 0; x < X_COUNT; ++x)
        {
            switch (data[y][x])
            {
            case 0:
                cout << " ";
                break;
            case 1:
                cout << ">";
                break;
            case 2:
                cout << "○";
                break;
            case 3:
                cout << "x";
                break;
            case 4:
                cout << "*";
                break;
            }
        }
        cout << "│" << endl;
    }

    for (int i = 0; i < X_COUNT; ++i)
    {
        cout << "▔";
    }
    cout << endl;
}

int randomRange1(int begin, int end)
{
    // static default_random_engine randomEngine(time(0));
    // uniform_int_distribution<unsigned> uniformGenerator(begin, end);
    // return uniformGenerator(randomEngine);

    return (rand() % (end - begin + 1) + begin);
}

struct BreadthPoint
{
    int x, y;
    BreadthPoint() : BreadthPoint(0, 0){};
    BreadthPoint(int x, int y) : x(x), y(y) {}
};

void breadthFirstSearch(const int (&data)[Y_COUNT][X_COUNT], const BreadthPoint &begin_point, const BreadthPoint &end_point)
{
    const int dir[4][2] = {{ 1,  1},
                           { 1, -1},
                           {-1,  1},
                           {-1, -1}};

    for (int y = begin_point.y; y < end_point.y; ++y)
    {
        for (int x = begin_point.x; x < end_point.x; ++x)
        {
            for(int i = 0; i < 4; ++i)
            {

            }
        }
    }
}

void breadthFirstSearchDemo()
{
    cout << "广度优先算法演示" << endl;
    srand(time(0));

    int data[Y_COUNT][X_COUNT];
    fill(begin(data[0]), end(data[Y_COUNT - 1]), 0);

    // 起点
    int begin_x = randomRange1(0, X_COUNT - 1);
    int begin_y = randomRange1(0, Y_COUNT - 1);
    cout << "起点：" << begin_x << "," << begin_y << endl;
    data[begin_y][begin_x] = 2;

    // 终点
    int end_x = randomRange1(0, X_COUNT - 1);
    int end_y = randomRange1(0, Y_COUNT - 1);
    cout << "终点：" << end_x << "," << end_y << endl;
    data[end_y][end_x] = 3;

    // 障碍
    int min_x = max<int>(min<int>(begin_x, end_x) + 1, 0);
    int min_y = max<int>(min<int>(begin_y, end_y) + 1, 0);
    int max_x = min<int>(max<int>(begin_x, end_x) - 1, X_COUNT - 1);
    int max_y = min<int>(max<int>(begin_y, end_y) - 1, Y_COUNT - 1);
    for (int i = 0; i < 10; ++i)
    {
        int x = randomRange1(min_x, max_x);
        int y = randomRange1(min_y, max_y);
        data[y][x] = 4;
    }

    showMapData1(data);

    // A-Star 算法寻路
    breadthFirstSearch(data, {begin_x, begin_y}, {end_x, end_y});

    showMapData1(data);
}